import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_module_sale/common/config/page_base_common.dart';
import 'package:dk_flutter_module_sale/config/route/common_widget_route.dart';
import 'package:dk_flutter_module_sale/phoenix/home/page/home_page.dart';
import 'package:dk_flutter_module_sale/phoenix/information/page/page_information.dart';
import 'package:dk_flutter_module_sale/phoenix/message/first_message/page/main_message_page.dart';
import 'package:dk_flutter_module_sale/phoenix/my_setting/page/me_setting_page.dart';
import '../../r.dart';
import 'package:flutter_custom_bottom_tab_bar/eachtab.dart';

@ARoute(url: CommonWidgetRoute.COMMON_REVISION_HOME)
class MainTabbarPage extends CommonPage {
  final BoostRouteOption option;
  MainTabbarPage(this.option) : super();
  @override
  void onInitState(CommonPageBloc pageBloc, {Map<String, Bloc> extraBlocMap}) {
    pageBloc.switchPage(TabbarWidget(pageBloc));
  }
}

class TabbarWidget extends StatefulWidget {
  final CommonPageBloc pageBloc;
  TabbarWidget(this.pageBloc);

  @override
  TabbarState createState() {
    return TabbarState(this.pageBloc);
  }
}

class TabbarState extends State<TabbarWidget> with SingleTickerProviderStateMixin {
  EventBus eventBus = EventBus();
  var _pageList;
  var _tarImages;
  var _appBarTitles = ['首页','资讯','消息','我的'];

  int _currentIndex = 0;
  TabController _tabController;
  DateTime _lastPressedAt; //上次点击时间
  CommonPageBloc messageBloc;
  TabbarState(this.messageBloc);

  @override
  void initState() {
    super.initState();
    initData();
  }

  void initData() {

    _pageList = [
      HomePage(this.eventBus),
      InformationPage(),
      MainMessagePage(clickTabBarEventBus: this.eventBus,),
      MySettingPage(clickTabBarEventBus: this.eventBus,),
    ];
    _tarImages = [
      [
        Image.asset(R.imagesTabbarUnselectedhomePng,width: 24,height: 24,fit:BoxFit.fill,),
        Image.asset(R.imagesTabbarSelectedhomePng,width: 24,height: 24,fit:BoxFit.fill),
      ],
      [
        Image.asset(R.imagesTabbarUnselectedinformationPng,width: 24,height: 24,fit:BoxFit.fill,),
        Image.asset(R.imagesTabbarSelectedinformationPng,width: 24,height: 24,fit:BoxFit.fill),

      ],
      [
        Image.asset(R.imagesTabbarUnselectmessagePng,width: 24,height: 24,fit:BoxFit.fill,),
        Image.asset(R.imagesTabbarSelectmessagePng,width: 24,height: 24,fit:BoxFit.fill),

      ],
      [
        Image.asset(R.imagesTabbarUnselectedmePng,width: 24,height: 24,fit:BoxFit.fill,),
        Image.asset(R.imagesTabbarSelectedmePng,width: 24,height: 24,fit:BoxFit.fill),
      ]
    ];

    _tabController = TabController(length: _appBarTitles.length, vsync: this,initialIndex: 0);
    _tabController.addListener((){
      if(_tabController.indexIsChanging == false) {
        setState(() {
          this.clickTapBar();
        });
      }
    });
  }
  void clickTapBar() {
    _currentIndex = _tabController.index;
    TabBarPage tabBarPage;
      switch(_currentIndex) {
        case 0:{
          tabBarPage = TabBarPage.TabBarPageHome;
          break;
        }
        case 1:{
          tabBarPage = TabBarPage.TabBarPageNews;
          break;
        }
        case 2:{
          tabBarPage = TabBarPage.TabBarPageMessage;
          break;
        }
        case 3:{
          tabBarPage = TabBarPage.TabBarPageMySetting;
          break;
        }
        default:{
          tabBarPage = TabBarPage.TabBarPageHome;
        }
      }

    if(tabBarPage == TabBarPage.TabBarPageMessage || tabBarPage == TabBarPage.TabBarPageMySetting || tabBarPage == TabBarPage.TabBarPageHome ){
      ClickTabBarEvent event = ClickTabBarEvent(clickPage: tabBarPage);
      this.eventBus.fire(event);
    }
  }

  Future<bool> pop() async {
    if (_lastPressedAt == null ||
        DateTime.now().difference(_lastPressedAt) > Duration(seconds: 1)) {
      //两次点击间隔超过1秒则重新计时
      _lastPressedAt = DateTime.now();
      return false;
    }
    return true;
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(child: SafeArea(
      top: false,
      bottom: true,
      child: Scaffold(
        bottomNavigationBar: Container(//底部导航条
          height: 52.0,
          color: Colors.white,
          child: Column(
            children: <Widget>[
              Divider(
                height: 1,
                color: Color(0xffebeeef),
              ),
              new TabBar(
                isScrollable: false,
                controller: _tabController,
                indicatorColor: Colors.transparent,
                labelColor: Color(0xff57698C),
                labelPadding: EdgeInsets.all(0),
                unselectedLabelColor: Color(0xff818998),
                tabs: setUpEachTabs(),
              ),
            ],
          ),
        ),
        body: TabBarView(//内容视图
          controller: _tabController,
          children: _pageList,
          physics: NeverScrollableScrollPhysics(),
        ),),
    ), onWillPop: pop);
  }

  List<Widget>  setUpEachTabs() {
    List<Widget> tabs = List<Widget>();
    for(int i = 0;i < _appBarTitles.length; i++) {
      tabs.add(EachTab(
          height: 45,
          padding: EdgeInsets.only(top: 4),
          icon: _currentIndex == i
              ? _tarImages[i][1] : _tarImages[i][0],
          text: _appBarTitles[i],
          textStyle: TextStyle(fontSize: 11)
      ));
    }
    return  tabs;
  }

  @override
  void dispose() {
    super.dispose();
  }
}

enum TabBarPage {
  TabBarPageHome,
  TabBarPageNews,
  TabBarPageMessage,
  TabBarPageMySetting
}

class ClickTabBarEvent {
  final TabBarPage clickPage;
  ClickTabBarEvent({this.clickPage});
}